package vsm.vsm_server.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.HistoryMapper;
import vsm.vsm_server.pojo.User.Record.History.CreateHistoryDTO;
import vsm.vsm_server.pojo.User.Record.History.HistoryVideo;
import vsm.vsm_server.pojo.User.Record.History.UserVideoHistoryDTO;
import vsm.vsm_server.pojo.User.Record.History.WatchDTO;
import vsm.vsm_server.pojo.Video.Video;
import vsm.vsm_server.pojo.Video.VideoHistoryVO;
import vsm.vsm_server.pojo.Video.VideoUrlVO;
import vsm.vsm_server.service.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Service
public class HistoryServiceImpl implements HistoryService {


    private HistoryMapper historyMapper;

    public HistoryServiceImpl(HistoryMapper historyMapper){
        this.historyMapper=historyMapper;
    }

    @Autowired
    VideoService videoService;
    @Autowired
    VodService vodService;
    @Autowired
    ActionService actionService;
    @Autowired
    LikeService likeService;

    @Override
    public List<VideoHistoryVO> getHistoryVideos(BigInteger userId) {
        List<VideoHistoryVO> videoNailVOList = new ArrayList<>();
        List<HistoryVideo> historyVideoList =  historyMapper.getHistoryVideos(userId);
        if(historyVideoList!=null){
            for(HistoryVideo v:historyVideoList){
                //根据videoId获取video信息
                Video video = videoService.getVideo(v.getVideoId());
                //根据video信息赋值给videoNail
                VideoHistoryVO newVideoNailVO = new VideoHistoryVO();
                BeanUtils.copyProperties(video, newVideoNailVO);
                //获取到videoNail其他值
                VideoUrlVO newVideoUrlVO = vodService.getUrl(newVideoNailVO.getPath());
                BeanUtils.copyProperties(newVideoUrlVO, newVideoNailVO);
                //将videoNail放入列表
                videoNailVOList.add(newVideoNailVO);
            }
        }
        return videoNailVOList;
    }

    @Override
    public HistoryVideo getHistory(BigInteger userId, BigInteger videoId) {
        return historyMapper.getHistory(userId,videoId);
    }

    @Override
    public void createHistoryVideo(WatchDTO watchDTO) {
        actionService.WatchVideo(watchDTO);


        CreateHistoryDTO createHistoryDTO = new CreateHistoryDTO();
        BeanUtils.copyProperties(watchDTO,createHistoryDTO);
        double finish = watchDTO.getCurrentTime()/ watchDTO.getDuration();
        if(watchDTO.getPlayTime()>=watchDTO.getDuration())
            finish = 1;
        createHistoryDTO.setFinished(finish);

        historyMapper.createOrUpdateHistory(createHistoryDTO);

    }

    @Override
    public Double getViewCount(BigInteger videoId) { //获取播放量
        return historyMapper.getViewCount(videoId);
    }
    @Override
    public List<UserVideoHistoryDTO> getAllHistoryVideos(int limit){
       return historyMapper.getAllHistoryVideos(limit);
    }

    @Override
    public List<BigInteger> getAllWatchedVideoIds(BigInteger userId) {
        List<HistoryVideo> historyVideoList =  historyMapper.getHistoryVideos(userId);
        List<BigInteger> historyVideoId = new ArrayList<>();
        for(HistoryVideo h:historyVideoList){
            BigInteger id = h.getVideoId();
            historyVideoId.add(id);
        }
        return historyVideoId ;
    }


}
